/**
 * Created by fuqiang on 2017/6/8.
 */

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function (req, res) {
    res.send('<h1>Hello world</h1>');
    // res.sendFile(__dirname + '/index.html')
});
var index = 0;
io.on('connection', function (socket) {
    console.log('%c 🍸 socket: ', 'font-size:20px;background-color: #B03734;color:#fff;', socket.connected);
    index++;
    var user = index + '号选手';
    console.log('用户连接！');
    // io.emit('chat message', user, user + '连接成功');
    // socket.broadcast.emit('hi');
    // io.emit('some event', { for: 'everyone' });
    socket.on('chat message', function (msg) {
        io.emit('chat message', user, msg);
        console.log('message: ' + msg);
    });
    socket.on('disconnect', function () {
        console.log('用户连接断开')
        io.emit('chat message', user, user + '连接断开');
    })
})
http.listen(30001, function () {
    console.log('listening on *:30001');
});
